﻿using Common.Contract;
using Common.Contract.Customer.Identifier;
using Common.Contract.Customer.Record;
using Common.Contract.DataCenter.Record;
using Common.Contract.DataCenter.Request;
using Common.Contract.SaleOut.Record;
using Common.Contract.Warranty.Record;
using Common.Service.Command;
using Common.Service.Queue;
using Common.Util;
using DataCenter.Configuration;
using DataCenter.Model.Interface;
using DataCenter.Model.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataCenter.ServiceCommand
{
    public class UpdatePndHistoryServiceCommand : ProviderQueueCommand<IProviderFactory, PndHistRecord, ResponseRecord>
    {
        public UpdatePndHistoryServiceCommand(IProviderFactory provider) : base(provider) { }
        protected override void Validate(PndHistRecord record)
        {
            if (record == null) throw new ArgumentNullException("request");
        }

        protected override bool OnExecute(ResponseRecord response)
        {
            var cdcHist = Record;
            var obj = new SerialNumberPndHist()
            {
                Moseq = cdcHist.Moseq,
                CfmDate = cdcHist.ConfirmDate,
                CfmFlag = cdcHist.ConfirmFlag,
                CfmType = cdcHist.ConfirmType,
                CfmUser = cdcHist.ConfirmUser,
                CmdCode = cdcHist.CmdCode,
                EndUserCell = cdcHist.EndUserCell,
                Model = cdcHist.Model,
                MoMsgbody = cdcHist.MessageBody,
                PndDays = cdcHist.PndDays,
                PndType = cdcHist.PndType,
                ReceiveDate = cdcHist.ReceiveDate,
                RejectDate = cdcHist.RejectDate,
                RejectFlag = cdcHist.RejectFlag.HasValue ? cdcHist.RejectFlag.Value.ToBooleanString() : null,
                RejectUser = cdcHist.RejectUser,
                RespMsg = cdcHist.ResponseMsg,
                RespType = cdcHist.ResponseType,
                SerialNo = cdcHist.SerialNumber,
                ShopCell = cdcHist.ShopCell,
                ShopCode = cdcHist.ShopCode,
                SmsYn = cdcHist.SmsYn.HasValue ? cdcHist.SmsYn.Value.ToBooleanString() : null,
                SoTransferDate = DateTime.Now,
                SoTransferFlag = BooleanString.TRUE,
            };
            var itemCheck = Providers.SerialNumberPndHist.RetrievePndHistory(Record.SerialNumber, Record.PndType);
            if (itemCheck == null)
            {
                var resultitem1 = Providers.SerialNumberPndHist.Add(obj);
            }
            else
            {
                var resultitem2 = Providers.SerialNumberPndHist.Update(obj);
            }
            return true;
        }
    }
}
